const clientUtils = {
	// 判断终端是否是app
	isAPP: function () {
		const u = navigator.userAgent.toLocaleLowerCase();
		// console.log(u, "userAgent");
		if (u.includes("wanyan")) {
			return true;
		} else {
			return false;
		}
	},

	// 是否是小程序
	isMiniProgram() {
		const u = navigator.userAgent.toLocaleLowerCase();
		if (u.includes("miniprogram") || u.includes("miniProgram")) {
			return true;
		} else {
			return false;
		}
	},

	// 判断是否是微信打开的h5
	isWeChat() {
		const u = navigator.userAgent;
		const isWeChatMicro =
			u.toLocaleLowerCase().indexOf("micromessenger") !== -1;
		if (isWeChatMicro) {
			return true;
		} else {
			return false;
		}
	},

	// 终端判断
	checkClient: function () {
		const u = navigator.userAgent;
		// console.log(navigator.userAgent, "navigator.userAgent");
		// android终端
		const isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
		// ios终端
		const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		if (isIOS) {
			return "ios";
		} else if (isAndroid) {
			return "android";
		} else {
			return "other";
		}
	},

	// app 版本判断
	appUpgrade: function (adrVs = 55, iosVs = 22, refVersion = false) {
		const u = navigator.userAgent;
		// console.log("==内嵌页UA==", u);
		if (u.indexOf("wanyan") > -1) {
			// android终端
			const isAndroid =
				u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
			// ios终端
			const isIOS = u.includes("ios");
			if (isAndroid) {
				const adr = /Android_wanyan_V(\d+)/.test(u);
				const adrVersion = Number(RegExp.$1 || 0); // IOS 版本号
				if (refVersion) return adrVersion;
				if (!adr || adrVersion < adrVs) {
					// 版本号小于 55 提示升级
					return true;
				} else {
					return false;
				}
			} else if (isIOS) {
				const ios = /ios_wanyan_V(\d+)|ios_baiyibao_V(\d+)/.test(u);
				const iosVersion = Number(RegExp.$1 || 0); // IOS 版本号
				if (refVersion) return iosVersion;
				if (!ios || iosVersion < iosVs) {
					// 版本号小于 22 提示升级
					return true;
				} else {
					return false;
				}
			}
		}
	},
	// 获取版本号
	appVersion: function () {
		const u = navigator.userAgent;
		// console.log("==内嵌页UA==", u);
		if (u.indexOf("wanyan") > -1) {
			// android终端
			const isAndroid =
				u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
			// ios终端
			const isIOS = u.includes("ios");
			if (isAndroid) {
				const adr = /Android_wanyan_V(\d+)/.test(u);
				const adrVersion = RegExp.$1 || 0; // IOS 版本号
				return adrVersion;
			} else if (isIOS) {
				const ios = /ios_wanyan_V(\d+)|ios_baiyibao_V(\d+)/.test(u);
				const iosVersion = RegExp.$1 || 0; // IOS 版本号
				return iosVersion;
			}
		} else {
			return "";
		}
	},
	/**
	 * 加载js
	 * @param url
	 */
	asyncLoadJs: async (url: string) => {
		const srcArr = document.getElementsByTagName("script");
		// console.log('srcArr====', srcArr)
		let hasLoaded = false;
		for (let i = 0; i < srcArr.length; i++) {
			// 判断当前js是否加载上
			console.log(
				"srcArr====",
				srcArr[i].src === url,
				srcArr[i].src,
				url
			);

			hasLoaded = srcArr[i].src === url ? true : false;
			if ((hasLoaded = srcArr[i].src === url)) {
				hasLoaded = true;
				return 2;
			}
		}
		// console.log("hasLoaded===", hasLoaded);
		if (hasLoaded) {
			return 2;
		}
		const script = document.createElement("script");
		script.src = url;
		script.type = "text/javascript";
		document.body.appendChild(script);
		return 1;
	},
	// 加载css
	asyncLoadCs: async (url: string) => {
		const css = document.createElement("link");
		css.href = url;
		css.rel = "stylesheet";
		css.type = "text/css";
		document.head.appendChild(css);
	},
	// 设置方法提供给app调用
	setAppInfo(callBack: any) {
		// if (this.isAPP()) {
		(window as any).window.setAppInfoFn = function (
			params: any,
			key: string
		) {
			// alert(key)
			// alert(params)
			// console.log('key===', key)
			// SessionCache.setItem(key, params)
			// if (key === 'goods') {
			// 	return callBack && callBack(params)
			// }
			return callBack && callBack(params);
		};

		// }
	},
};
export default clientUtils;
